package cn.qf.spring.quartz.jobs;

import org.quartz.*;
import org.quartz.impl.StdScheduler;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Date;

/**
 * 我的任务
 */
public class MyJob implements Job {

//    /**
//     * 任务定时5秒调用
//     * @param args
//     * @throws InterruptedException
//     * @throws SchedulerException
//     */
//    public static void main(String[] args) throws InterruptedException, SchedulerException {
//        // 工厂启动，任务启动，工厂关闭，任务停止
//        ApplicationContext context = new ClassPathXmlApplicationContext("spring-quartz.xml");
//    }

//    /**
//     * 定时3000ms，进行删除任务
//     * @param args
//     * @throws InterruptedException
//     * @throws SchedulerException
//     */
//    public static void main(String[] args) throws InterruptedException, SchedulerException {
//        ApplicationContext context = new ClassPathXmlApplicationContext("spring-quartz.xml");
//        System.out.println("=============");
//        StdScheduler scheduler = (StdScheduler) context.getBean("scheduler");
//        System.out.println(scheduler.getClass());
//        Thread.sleep(3000);
//        // 删除Job
//        scheduler.deleteJob(JobKey.jobKey("job1","job_group1"));
//    }

//    /**
//     * 暂停和恢复任务调度
//     * @param args
//     * @throws InterruptedException
//     * @throws SchedulerException
//     */
//    public static void main(String[] args) throws InterruptedException, SchedulerException {
//        ApplicationContext context = new ClassPathXmlApplicationContext("spring-quartz.xml");
//        System.out.println("=============");
//        StdScheduler scheduler = (StdScheduler) context.getBean("scheduler");
//        System.out.println(scheduler.getClass());
//        Thread.sleep(3000);
//        // 暂停，恢复工作
//
//        /**
//         * // 暂停工作
//         */
//        scheduler.pauseJob(JobKey.jobKey("job1","job_group1"));
//        Thread.sleep(3000);
//
//        /**
//         * // 恢复工作
//         */
//        scheduler.resumeJob(JobKey.jobKey("job1","job_group1"));
//    }

    /**
     * 暂停和恢复任务组调度
     * @param args
     * @throws InterruptedException
     * @throws SchedulerException
     */
    public static void main(String[] args) throws InterruptedException, SchedulerException {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-quartz.xml");
        System.out.println("=============");
        StdScheduler scheduler = (StdScheduler) context.getBean("scheduler");
        System.out.println(scheduler.getClass());
        Thread.sleep(3000);
        GroupMatcher<JobKey> group1 = GroupMatcher.groupEquals("group1");
        // 暂停组中所有工作
        scheduler.pauseJobs(group1);
        Thread.sleep(2000);
        // 恢复组中所有工作
        scheduler.resumeJobs(group1);
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.err.println("job 执行"+new Date());
    }
}
